<template>
  <div>
    <el-card class="filter-container" shadow="never">
      <div>
        <i class="el-icon-search"></i>
        <span>筛选搜索</span>
        <el-button
          style="float: right"
          @click="handleSearchList()"
          type="primary"
          size="small"
        >
          查询结果
        </el-button>
        <el-button
          style="float: right; margin-right: 15px"
          @click="handleResetSearch()"
          size="small"
        >
          重置
        </el-button>
      </div>
      <div style="margin-top: 15px">
        <el-form
          :inline="true"
          :model="searchList"
          size="small"
          label-width="140px"
        >
          <el-form-item label="需求id:">
            <el-input
              style="width: 203px"
              v-model="searchList.demandId"
              placeholder="请输入要搜索的需求id"
              clearable
            ></el-input>
          </el-form-item>

          <el-form-item label="服务id:">
            <el-input
              style="width: 203px"
              v-model="searchList.serverId"
              placeholder="请输入要搜索的服务id"
              clearable
            ></el-input>
          </el-form-item>

          <el-form-item label="状态:">
            <el-select
              v-model="searchList.status"
              style="width: 203px"
              placeholder="请选择订单状态"
              clearable
            >
              <el-option
                v-for="(item, index) in statusOptions"
                :key="index"
                :value="item.value"
                :label="item.label"
              >
              </el-option>
            </el-select>
          </el-form-item>

          <el-form-item label="服务开始时间:">
            <el-date-picker
              v-model="searchList.startTime"
              style="width: 203px"
              type="datetime"
              placeholder="选择日期时间"
              clearable
            >
            </el-date-picker>
          </el-form-item>

          <el-form-item label="服务结束时间:">
            <el-date-picker
              v-model="searchList.endTime"
              style="width: 203px"
              type="datetime"
              placeholder="选择日期时间"
              clearable
            >
            </el-date-picker>
          </el-form-item>
        </el-form>
      </div>
    </el-card>
    <div>
      <el-dialog :visible.sync="dialogVisible" title="订单详情" width="60%">
        <span>
          <el-card class="filter-container" shadow="never">
            <div style="margin-top: 15px">
              <el-form
                :inline="true"
                :model="detail"
                size="small"
                label-width="140px"
              >
                <el-form-item label="地址:">
                  <el-input
                    v-model="detail.address"
                    style="width: 203px"
                    disabled
                  ></el-input>
                </el-form-item>

                <el-form-item label="订单号:">
                  <el-input
                    v-model="detail.orderId"
                    style="width: 203px"
                    disabled
                  ></el-input>
                </el-form-item>

                <el-form-item label="公司名称:">
                  <el-input
                    v-model="detail.companyName"
                    style="width: 203px"
                    disabled
                  ></el-input>
                </el-form-item>

                <el-form-item label="需求:">
                  <el-input
                    v-model="detail.description"
                    style="width: 203px"
                    disabled
                  ></el-input>
                </el-form-item>

                <el-form-item label="更新时间:">
                  <el-input
                    v-model="detail.updateTime"
                    style="width: 203px"
                    disabled
                  ></el-input>
                </el-form-item>

                <!-- <el-form-item label="服务ID:">
                  <el-input
                    v-model="detail.serverId"
                    style="width: 203px"
                    disabled
                  ></el-input>
                </el-form-item> -->
                <!-- 
                <el-form-item label="顾客ID:">
                  <el-input
                    v-model="detail.userId"
                    style="width: 203px"
                    disabled
                  ></el-input>
                </el-form-item> -->

                <el-form-item label="顾客名:">
                  <el-input
                    v-model="detail.customerName"
                    style="width: 203px"
                    disabled
                  ></el-input>
                </el-form-item>

                <!-- <el-form-item label="公司ID:">
                  <el-input
                    v-model="detail.companyId"
                    style="width: 203px"
                    disabled
                  ></el-input>
                </el-form-item> -->

                <!-- <el-form-item label="需求ID:">
                  <el-input
                    v-model="detail.demandId"
                    style="width: 203px"
                    disabled
                  ></el-input>
                </el-form-item> -->

                <el-form-item label="创建时间:">
                  <el-input
                    v-model="detail.createTime"
                    style="width: 203px"
                    disabled
                  ></el-input>
                </el-form-item>

                <el-form-item label="联系电话:">
                  <el-input
                    v-model="detail.phone"
                    style="width: 203px"
                    disabled
                  ></el-input>
                </el-form-item>

                <el-form-item label="金额:">
                  <el-input
                    v-model="detail.price"
                    style="width: 203px"
                    disabled
                  ></el-input>
                </el-form-item>
                <el-form-item label="数量:">
                  <el-input
                    v-model="detail.qty"
                    style="width: 203px"
                    disabled
                  ></el-input>
                </el-form-item>

                <el-form-item label="开始服务日期:">
                  <el-input
                    v-model="detail.startTime"
                    style="width: 203px"
                    disabled
                  ></el-input>
                </el-form-item>

                <el-form-item label="状态:">
                  <el-tag
                    type="info"
                    effect="light"
                    v-if="detail.status == 'REFUND'"
                    >已退款</el-tag
                  >
                  <el-tag
                    type="info"
                    effect="plain"
                    v-else-if="detail.status == 'CANCEL'"
                    >已取消</el-tag
                  >
                  <el-tag
                    type=""
                    effect="plain"
                    v-else-if="detail.status == 'ORDER'"
                    >已下单</el-tag
                  >
                  <el-tag
                    type="success"
                    effect="plain"
                    v-else-if="detail.status == 'DISPATCH'"
                    >已分派人员</el-tag
                  >
                  <el-tag
                    type="danger"
                    effect="plain"
                    v-else-if="detail.status == 'GOING'"
                    >进行中</el-tag
                  >
                  <el-tag
                    type="danger"
                    effect="plain"
                    v-else-if="detail.status == 'PAYING'"
                    >待支付</el-tag
                  >
                  <el-tag
                    type="warning"
                    effect="plain"
                    v-else-if="detail.status == 'ENDED'"
                    >已结束</el-tag
                  >
                </el-form-item>
              </el-form>
            </div>
          </el-card>
        </span>
      </el-dialog>
    </div>
    <el-card style="margin-top: 15px" class="operate-container" shadow="never">
      <i class="el-icon-tickets"></i>
      <span>数据列表</span>
    </el-card>
    <div style="margin-top: 15px" class="table-container">
      <el-table
        :data="orderList"
        style="width: 100%"
        v-loading="listLoading"
        border
      >
        <el-table-column label="订单号" width="150" align="center">
          <template slot-scope="scope">{{ scope.row.orderId }}</template>
        </el-table-column>
        <el-table-column label="地址" width="150" align="center">
          <template slot-scope="scope">{{ scope.row.address }}</template>
        </el-table-column>
        <!-- <el-table-column label="公司名称" width="150" align="center">
          <template slot-scope="scope">{{ scope.row.companyName }}</template>
        </el-table-column> -->
        <el-table-column label="需求" width="150" align="center">
          <template slot-scope="scope">{{ scope.row.description }}</template>
        </el-table-column>
        <!-- <el-table-column label="更新时间" width="160" align="center">
          <template slot-scope="scope">{{ scope.row.updateTime }}</template>
        </el-table-column> -->
        <!-- <el-table-column label="服务ID" width="150" align="center">
          <template slot-scope="scope">{{ scope.row.serverId }}</template>
        </el-table-column> -->
        <!-- <el-table-column label="顾客ID" width="150" align="center">
          <template slot-scope="scope">{{ scope.row.userId }}</template>
        </el-table-column> -->
        <el-table-column label="顾客名" width="150" align="center">
          <template slot-scope="scope">{{ scope.row.customerName }}</template>
        </el-table-column>
        <!-- <el-table-column label="公司ID" width="150" align="center">
          <template slot-scope="scope">{{ scope.row.companyId }}</template>
        </el-table-column> -->
        <!-- <el-table-column label="需求ID" width="150" align="center">
          <template slot-scope="scope">{{ scope.row.demandId }}</template>
        </el-table-column> -->
        <!-- <el-table-column label="创建时间" width="160" align="center">
          <template slot-scope="scope">{{ scope.row.createTime }}</template>
        </el-table-column> -->
        <el-table-column label="联系电话" width="150" align="center">
          <template slot-scope="scope">{{ scope.row.phone }}</template>
        </el-table-column>
        <el-table-column label="金额" width="150" align="center">
          <template slot-scope="scope">{{ scope.row.price }}</template>
        </el-table-column>
        <el-table-column label="数量" width="150" align="center">
          <template slot-scope="scope">{{ scope.row.qty }}</template>
        </el-table-column>
        <el-table-column label="开始服务日期" width="160" align="center">
          <template slot-scope="scope">{{ scope.row.startTime }}</template>
        </el-table-column>
        <el-table-column label="状态" width="150" align="center">
          <template slot-scope="scope">
            <el-tag
              type="info"
              effect="light"
              v-if="scope.row.status == 'REFUND'"
              >已退款</el-tag
            >
            <el-tag
              type="info"
              effect="plain"
              v-else-if="scope.row.status == 'CANCEL'"
              >已取消</el-tag
            >
            <el-tag
              type=""
              effect="plain"
              v-else-if="scope.row.status == 'ORDER'"
              >已下单</el-tag
            >
            <el-tag
              type="success"
              effect="plain"
              v-else-if="scope.row.status == 'DISPATCH'"
              >已分派人员</el-tag
            >
            <el-tag
              type="danger"
              effect="plain"
              v-else-if="scope.row.status == 'GOING'"
              >进行中</el-tag
            >
            <el-tag
              type="danger"
              effect="plain"
              v-else-if="scope.row.status == 'PAYING'"
              >待支付</el-tag
            >
            <el-tag
              type="warning"
              effect="plain"
              v-else-if="scope.row.status == 'ENDED'"
              >已结束</el-tag
            >
          </template>
        </el-table-column>

        <el-table-column fixed="right" label="操作" width="300" align="center">
          <template slot-scope="scope">
            <el-button size="mini" type="info" @click="viewOrder(scope.row)"
              >订单详情</el-button
            >
            <el-button
              size="mini"
              type="warning"
              @click="cancelOrder(scope.row)"
              v-if="scope.row.status == 'ORDER'"
              >取消订单</el-button
            >
            <el-button
              size="mini"
              type="primary"
              @click="assignOrder(scope.row)"
              v-if="scope.row.status == 'ORDER'"
              >分配订单</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="block">
      <el-pagination
        @current-change="handleCurrentChange"
        layout="prev, pager, next"
        :page-count="pageCunt"
        :current-page.sync="currentPage"
      >
      </el-pagination>
    </div>
  </div>
</template>

<script>
export default {
  name: "managementOrder",
  data() {
    return {
      dialogVisible: false,
      pageCunt: 1,
      // messageDialogVisible: false,

      searchList: {
        demandId: "",
        endTime: "",
        pageNum: "1",
        pageSize: "5",
        serverId: "",
        startTime: "",
        status: "",
      },
      currentPage: 1,
      currentOrderList: {
        pageNum: "1",
        pageSize: "5",
      },
      detail: {},
      orderList: [
        {
          address: "",
          orderId: "",
          companyName: "",
          description: "",
          updateTime: "",
          serverId: "",
          userId: "",
          customerName: "",
          companyId: "",
          demandId: "",
          createTime: "",
          phone: "",
          price: "",
          qty: "",
          startTime: "",
          status: "",
        },
      ],
      serviceNameOptions: [{ name: "", id: "", status: "" }],
      statusOptions: [
        { type: "", value: "REFUND", label: "已退款" },
        { type: "", value: "CANCEL", label: "已取消" },
        { type: "", value: "ORDER", label: "已下单" },
        { type: "", value: "DISPATCH", label: "已分派人员" },
        { type: "", value: "GOING", label: "进行中" },
        { type: "", value: "PAYING", label: "待支付" },
        { type: "", value: "ENDED", label: "已结束" },
      ],
      listLoading: true,
    };
  },
  created() {
    this.getList();
  },
  methods: {
    getList() {
      // 发送请求获取订单列表
      // ...
      this.listLoading = true;
      this.axios
        .get("/api/company/auth/order/list", {
          params: this.currentOrderList,
        })
        .then((response) => {
          // console.log(response);
          if (response.data.code == "200") {
            this.orderList = response.data.value.data;

            if (
              response.data.value.total / 5 >
              Number.parseInt(response.data.value.total / 5)
            ) {
              this.pageCunt =
                Number.parseInt(response.data.value.total / 5) + 1;
            } else {
              this.pageCunt = Number.parseInt(response.data.value.total / 5);
            }
            // this.currentPage = 1;
          } else {
            this.$message.error(response.data.message);
          }
          this.listLoading = false;
        })
        .catch((error) => {
          this.$message.error("无法获取数据");
          this.listLoading = false;
        });
    },
    handleSearchList() {
      this.listLoading = true;
      this.axios
        .get("/api/company/auth/order/list", {
          params: this.searchList,
        })
        .then((response) => {
          // console.log(response);
          if (response.data.code == "200") {
            this.orderList = response.data.value.data;

            if (
              response.data.value.total / 5 >
              Number.parseInt(response.data.value.total / 5)
            ) {
              this.pageCunt =
                Number.parseInt(response.data.value.total / 5) + 1;
            } else {
              this.pageCunt = Number.parseInt(response.data.value.total / 5);
            }
            this.currentPage = 1;
          } else {
            this.$message.error(response.data.message);
          }
          this.listLoading = false;
        })
        .catch((error) => {
          this.$message.error("无法获取数据");
          this.listLoading = false;
        });
    },
    handleResetSearch() {
      this.searchList = {
        serverId: "",
        demandId: "",
        status: "",
        startTime: "",
        endTime: "",
      };
    },
    // 换页
    handleCurrentChange(val) {
      this.listLoading = true;
      this.currentOrderList.pageNum = val;
      this.axios
        .get("/api/company/auth/order/list", {
          params: this.currentOrderList,
        })
        .then((response) => {
          if (response.data.code == "200") {
            this.orderList = response.data.value.data;
          } else {
            this.$message.error(response.data.message);
          }
          this.listLoading = false;
        })
        .catch((error) => {
          this.message.error("无法获取数据");
          this.listLoading = false;
        });
    },
    getOrderStatusText(status) {
      switch (status) {
        case "REFUND":
          return "已退款";
        case "CANCEL":
          return "已取消";
        case "ORDER":
          return "已下单";
        case "DISPATCH":
          return "已分派人员";
        case "GOING":
          return "进行中";
        case "PAYING":
          return "待支付";
        case "ENDED":
          return "已结束";
        default:
          return "";
      }
    },
    viewOrder(row) {
      // 查看订单详情
      // ...
      this.dialogVisible = true;
      this.detail = row;
    },
    cancelOrder(row) {
      // 取消订单
      // ...
      this.$confirm("请再次确认是否取消订单").then(() => {
        this.axios
          .get("/api/company/auth/order/cancel?orderId=" + row.orderId)
          .then((response) => {
            if (response.data.code == "200") {
              this.$message({
                message: "订单取消成功",
                type: "success",
              });
              this.getList();
            } else {
              this.$message.error(response.data.message);
            }
          })
          .catch((error) => {
            this.$message.error("订单取消失败");
          });
      });
    },
    assignOrder(row) {
      // 分配订单
      this.axios
        .get("/api/company/auth/order/dispatch?orderId=" + row.orderId)
        .then((response) => {
          if (response.data.code == "200") {
            this.$message({
              message: "订单分配成功",
              type: "success",
            });
            this.getList();
          } else {
            this.$message.error(response.data.message);
          }
        })
        .catch((error) => {
          this.$message.error("订单分配失败");
        });
    },
    getserviceNameOptions() {
      this.axios
        .get("api/company/auth/label/list")
        .then((response) => {
          if (response.data.code == "200") {
            // this.ServiceList = response.data.value.data;
            this.serviceNameOptions = response.data.value;
            this.serviceNameOptions = this.serviceNameOptions.filter(function (
              item
            ) {
              if (item.status == "NORMAL") return item;
            });
          }
        })
        .catch((error) => {
          this.message.error("无法获取数据");
        });
    },
  },
};
</script>
